package com.offcn.user.service.impl;

import com.offcn.user.mapper.TMemberMapper;
import com.offcn.user.pojo.TMember;
import com.offcn.user.pojo.TMemberExample;
import com.offcn.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TMemberMapper memberMapper;

    @Override
    public void registerUser(TMember member) {
        // 1、检查系统中此手机号是否存在
        TMemberExample example = new TMemberExample();
        example.createCriteria().andLoginacctEqualTo(member.getLoginacct());
//        long l = memberMapper.countByExample(example);
//        if (l > 0) {
//            throw new UserException(UserExceptionEnum.LOGINACCT_EXIST);
//        }
        // 2、手机号未被注册，设置相关参数，保存注册信息
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(member.getUserpswd());
        //设置密码
        member.setUserpswd(encode);
        member.setUsername(member.getLoginacct());//默认把手机号当做用户名了
        member.setEmail(member.getEmail());
        //实名认证状态 0 - 未实名认证， 1 - 实名认证申请中， 2 - 已实名认证
        member.setAuthstatus("0");
        //账户类型: 0 - 企业， 1 - 个体， 2 - 个人， 3 - 政府
        member.setAccttype("2");
        System.out.println("插入数据:"+member.getLoginacct());
        memberMapper.insertSelective(member);
    }


    @Override
    public TMember login(String username, String password) {

        TMemberExample example = new TMemberExample();
        TMemberExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);//username其实就是手机号
        List<TMember> tMembers = memberMapper.selectByExample(example);
        if(tMembers!=null && tMembers.size()==1){
            TMember member = tMembers.get(0);
            //账号是存在的，再来校验密码是否正确
            String userpswd = member.getUserpswd();//数据库中密文密码
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean matches = encoder.matches(password, userpswd);
            if(matches){
                return member;
            }
        }
        return null;
    }

    @Override
    public TMember findMemberById(Integer id) {
        return memberMapper.selectByPrimaryKey(id);
    }
}
